package com.invengo.base.base;

import android.app.Application;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.invengo.base.BuildConfig;
import com.invengo.library_db.GreenDaoManager;

import io.reactivex.plugins.RxJavaPlugins;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.Utils;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;
import me.jessyan.retrofiturlmanager.onUrlChangeListener;
import okhttp3.HttpUrl;

/**
 * Created by goldze on 2018/6/21 0021.
 * 基础库自身初始化操作
 */

public class BaseModuleInit implements IModuleInit {
    @Override
    public boolean onInitAhead(Application application) {
        RxJavaPlugins.setErrorHandler(throwable -> {
//            ToastUtils.showShort(throwable.getLocalizedMessage());
        });
        //初始化工具类
        Utils.init(application);
        application.registerActivityLifecycleCallbacks(new BaseActivityLifecycleCallbackImpl());
        //开启打印日志
        KLog.init(true);
        //初始化阿里路由框架
        if (BuildConfig.DEBUG) {
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ACache.init(application);
        ARouter.init(application); // 尽可能早，推荐在Application中初始化

        initRetrofitManager();
        return false;
    }


    @Override
    public boolean onInitLow(Application application) {
//        KLog.e("基础层初始化 -- onInitLow");
        return false;
    }



    private void initRetrofitManager() {
        RetrofitUrlManager.getInstance().registerUrlChangeListener(new onUrlChangeListener() {
            @Override
            public void onUrlChangeBefore(HttpUrl oldUrl, String domainName) {
            }

            @Override
            public void onUrlChanged(HttpUrl newUrl, HttpUrl oldUrl) {
                Log.i("URLChange", "┌────── 原始地址 ────────────────────────────────────────────────────────────────────────");
                Log.i("URLChange", "| 默认地址：" + oldUrl + " \n 真实地址：" + newUrl);
                Log.i("URLChange", "└───────────────────────────────────────────────────────────────────────────────────────");
            }
        });
    }


}
